﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Xml;
using System.Text;
using WebApp.CompanyService;
using WebApp.DomainService;
using WebApp.ProductService;
using WebApp.TypeService;

namespace WebApp
{
    public partial class HotProduct : System.Web.UI.Page
    {
        ProductServiceClient ProductClient;
        TypeServiceClient TypeClient;
        DomainServiceClient DomainClient;
        CompanyServiceClient CompanyClient;
        protected void Page_Load(object sender, EventArgs e)
        {
            ProductClient = new ProductServiceClient();
            TypeClient = new TypeServiceClient();
            DomainClient = new DomainServiceClient();
            CompanyClient = new CompanyServiceClient();
            List<ProductDTO> newlist = new List<ProductDTO>();
            newlist = GetAllProduct();
            WriteXMLToFile(newlist);
            DataSet ds = new DataSet();
            ds.ReadXml(Server.MapPath("App_Data/HotProduct.xml"));
            ASPxDataView1.DataSourceID = null;
            ASPxDataView1.DataSource = ds;
            ASPxDataView1.DataBind();
        }
        private List<ProductDTO> GetAllProduct()
        {
            List<ProductDTO> newlist = new List<ProductDTO>();
            newlist.AddRange(ProductClient.GetAllProducts());
            return newlist;

        }
        
        private void WriteXMLToFile(List<ProductDTO> List)
        {
            XmlTextWriter xmlwriter = new XmlTextWriter(Server.MapPath("App_Data/HotProduct.xml"), Encoding.UTF8);
            xmlwriter.Formatting = Formatting.Indented;
            xmlwriter.WriteStartDocument();
            xmlwriter.WriteComment("Ghi dữ liệu ra XML");
            xmlwriter.WriteStartElement("NewProduct");

            //Duyet tat ca san pham

            foreach (ProductDTO item in List)
            {
                xmlwriter.WriteStartElement("HotProduct");
                xmlwriter.WriteAttributeString("ProductName", item.ProductName);
                xmlwriter.WriteAttributeString("ProductID", item.ProductID.ToString());
                xmlwriter.WriteElementString("Image", item.Image);
                xmlwriter.WriteElementString("Price", item.Price.ToString());
                xmlwriter.WriteElementString("ProPrice", item.ProPrice.ToString());
                xmlwriter.WriteEndElement();
            }

            xmlwriter.WriteEndElement();
            xmlwriter.WriteEndDocument();
            xmlwriter.Flush();
            xmlwriter.Close();
        }
    }
}